package com.nbg.web;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.MethodNotSupportedException;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HttpContext;

import com.nbg.database.FinishBean;
import com.nbg.database.MonitorDB;
import com.nbg.monitor.MainApplication;

public class DataHandler extends AbstractHandler {

	public void handle(final HttpRequest request, final HttpResponse response,
			final HttpContext context) throws HttpException, IOException {
		String method = request.getRequestLine().getMethod()
				.toUpperCase(Locale.ENGLISH);
		Map<String, String> params=getParam(request);
		int normal=0;
		int warn=0;
		int error=0;
		if (method.equals("GET")) {
			response.setStatusCode(HttpStatus.SC_OK);
			StringBuilder monitor = new StringBuilder();
			String day=params.get("day");
			if (day == null) {
				day = new SimpleDateFormat("yyyyMMdd", Locale.CHINA)
						.format(new Date());
			}
			int a = 0;
			try {
				a = Integer.parseInt(day);
			} catch (Exception e) {
				e.printStackTrace();
			}
			int b=0;
			System.out.println("load data a:"+a);
			List<FinishBean> finishBeans = MonitorDB.getInstance().load(a);
			for (int i=0;i<finishBeans.size();i++) {
				FinishBean finishBean=finishBeans.get(i);
				if (b != 0 && b != finishBean.getDay()) {
					monitor.append("------------------*********************--------------\n\n");
				}
				if(i<9){
					monitor.append("0");
				}
				monitor.append(i+1).append(")").append(finishBean.getDay());
				monitor.append("|").append(finishBean.getClient_ip());
				monitor.append("|").append(finishBean.getSoft_version());
				monitor.append("|").append(finishBean.getStep());
				monitor.append("|").append(finishBean.getFail());
				long ago=System.currentTimeMillis()-finishBean.getTime();
				if(ago<60000){
					monitor.append("\t正常");
					normal++;
				}else if (ago<600000) {
					monitor.append("\t警告");
					warn++;
				}else {
					monitor.append("\t错误");
					error++;
				}				
				monitor.append("\tNUM:").append(finishBean.getNum());
				monitor.append("\t").append(finishBean.getProduct());
				monitor.append("\t").append(finishBean.getPkg());
				monitor.append("\t").append(finishBean.getSearch());
//				monitor.append("\t").append(finishBean.getImei());
				monitor.append("\t").append(
						MonitorHandler.formatTimeAgo(finishBean.getTime()));
				monitor.append("\n");
				b = finishBean.getDay();
			}
			long reboot_time = (System.currentTimeMillis() - MainApplication.getContext().getSharedPreferences("reboot", 0)
					.getLong("last_reboot_time", 0)) / 60000;
			monitor.insert(0, "状态：   正常("+normal+")\t警告("+warn+")\t错误("+error+")    "+reboot_time+"m  \n\n");
			
			if(normal==0&&warn==0){
				monitor.insert(0, "严重错误状态，请抓紧联系你老公，注意暗号\n\n");
			}
			StringEntity entity = new StringEntity(monitor.toString(), "utf-8");
			response.setEntity(entity);
		} else {
			throw new MethodNotSupportedException(method
					+ " method not supported");
		}
	}
}
